package servlets;

import entities.UserEntity;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import utils.EmailChecker;
import utils.MD5Coder;

/**
 * 
 * @author andrei costache
 *
 */
public class UserEditController extends UserController {

	@Override
	public void doRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
           super.doRequest(request, response);
           RequestDispatcher view = null;
           String submitted = request.getParameter("submit");
           String redirect = null;
            if (submitted != null){
                String first_name = request.getParameter("first_name");
                String last_name = request.getParameter("last_name");
                String email = request.getParameter("email");
                String pass = request.getParameter("password");
                boolean ok = true;
                if (first_name == null || first_name.trim().equals("")){
                    msg.addStatusMessage("Please enter a First name");
                    ok = false;
                }
                if (last_name == null || last_name.trim().equals("")){
                    msg.addStatusMessage("Please enter a Last name");
                    ok = false;
                }
                if (email == null || email.trim().equals("")){
                    msg.addStatusMessage("Please enter an Email");
                    ok = false;
                } else if(!EmailChecker.isEmailValid(email)){
                    msg.addStatusMessage("The email address is invalid!");
                    ok = false;
                }
                if (ok){
                    HttpSession session = request.getSession();
                    UserEntity logged_user = (UserEntity)session.getAttribute("user");
                    UserEntity user = userDAO.find(logged_user.getUid());
                    user.setFirstname(first_name);
                    user.setLastname(last_name);
                    user.setMail(email);
                    user.setChanged(System.currentTimeMillis()/1000);
                    if (!pass.trim().equals("")){
                        user.setPassword(MD5Coder.getEncodedText(pass));
                    }
                    userDAO.edit(user);
                    session.setAttribute("user", user);
                    msg.addStatusMessage("The account has been updated");
                    redirect ="/user/profile" ;
                }
            }
           request.setAttribute("messages", msg);
           if (redirect != null){
               response.sendRedirect(request.getContextPath()+redirect);
           } else {
               view = request.getRequestDispatcher("/jsp/UserEditProfile.jsp");
               view.forward(request, response);
            }
        }
}
